package com.v1.video.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.v1.video.util.WindowUtils;

public class StretchLinearLayout extends LinearLayout {

	private Context mContext;
	private LayoutInflater mInflater;
	private int mScreenWidth;
	private double mPicWHRate = 3.0 / 4.0;
	private double mPicHeight;

	private int mCol = 2;// 显示的列数
	private int mDefaultShowItemNum = 2;// 默认显示的个数

	private int mShowedCount = 0;//已经显示的个数
	private int mItemCount;//总共需要显示的个数
	private int mItemViewResID;// 显示的布局对象

	private float mMargin = 7.0f;// 间隔

	private float mDensity;
	
	private LinearLayout.LayoutParams mItemLayoutParams;//单元格的布局参数
	private LinearLayout.LayoutParams mParentLayoutParams;//单元格父容器布局参数
	
	private OnItemRenderListener mOnItemRenderListener;//渲染itemview事件
	
	public void setOnItemRenderListener(OnItemRenderListener onItemRenderListener) {
		this.mOnItemRenderListener = onItemRenderListener;
	}

	public void setPicWHRate(double picWHRate) {
		this.mPicWHRate = picWHRate;
	}
	
	public interface OnItemRenderListener{
		public void onRender(int index,View view);
	}
	
	public void setItemCount(int itemCount) {
		this.mItemCount = itemCount;
	}
	
	public void setDefaultShowItemNum(int defaultShowItemNum) {
		this.mDefaultShowItemNum = defaultShowItemNum;
	}
	
	public int getShowedCount() {
		return mShowedCount;
	}
	
	public void setItemViewResID(int itemViewResID) {
		this.mItemViewResID = itemViewResID;
	}
	
	public void setMargin(float margin) {
		this.mMargin = margin;
	}
	
	public StretchLinearLayout(Context context) {
		super(context);
		init(context);
	}

	public StretchLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}
	
	//重置状态
	public void reset(){
		mCol = 2;// 显示的列数
		mDefaultShowItemNum = 2;// 默认显示的个数

		mShowedCount = 0;//已经显示的个数
		mItemCount = 0;//总共需要显示的个数
		mItemViewResID = 0;// 显示的布局对象

	    mMargin = 7.0f;// 间隔
	}

	private void init(Context context) {
		this.setOrientation(LinearLayout.VERTICAL);
		mContext = context;
		mMargin = mMargin * mDensity;
		mDensity = WindowUtils.getDensity(mContext);
		mScreenWidth = WindowUtils.getScreenWidth(mContext);
		mInflater = LayoutInflater.from(mContext);
	}
	
	private void initData() {		
		double picWidth = (mScreenWidth - 3 * mMargin) / mCol;
		mPicHeight = picWidth * mPicWHRate;
		
		mItemLayoutParams = new LinearLayout.LayoutParams((int) picWidth,(int) (mPicHeight));
		mItemLayoutParams.setMargins((int)(mMargin), (int)(mMargin), 0, 0);
		mParentLayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
		mParentLayoutParams.setMargins(0, 0, (int)(mMargin), 0);
	}
	
	public void show(int num){
		initData();
		if(num <= this.mDefaultShowItemNum){
			showOtherItem(mDefaultShowItemNum);
		}else{
			showOtherItem(num);
		}
	}

	/**
	 * 再显示几个元素，必须是列数的倍数
	 * 
	 * @param num
	 *            必须是列数的倍数，否则控件会自动调整
	 */
	public void showOtherItem(int num) {
		int showNum = num;
		
		if(showNum <= 0 || mShowedCount == mItemCount){
			return ;
		}
		
		/*if(showNum % mCol != 0){
			double sang = (double)showNum / mCol;
			showNum = (int)(sang +0.5); 
		}*/
		
		if(mShowedCount+showNum <= mItemCount){//请求的个数全部显示
			mShowedCount += showNum;
		}else{//请求的个数多余剩余的个数，将剩下的全部显示
			showNum = mItemCount - mShowedCount;
			mShowedCount = mItemCount;
		}
		
		LinearLayout horLay = null;
		
		for(int i=0;i<showNum;i++){
			View itemView = mInflater.inflate(mItemViewResID, null);
			if(mOnItemRenderListener !=null){
				mOnItemRenderListener.onRender(mShowedCount-showNum+i,itemView);
			}
			if(i % mCol == 0){//换行
				horLay = new LinearLayout(mContext);
				horLay.setOrientation(LinearLayout.HORIZONTAL);
				horLay.addView(itemView, mItemLayoutParams);
				this.addView(horLay,mParentLayoutParams);
			}else{//直接添加
				horLay.addView(itemView, mItemLayoutParams);
			}
		}
	}

	/**
	 * 隐藏所有展开的其它元素
	 */
	public void hideAllOtherItem() {
		this.removeAllViews();
		mShowedCount = 0;
		showOtherItem(mDefaultShowItemNum);
	}

	/*
	 * public StretchLinearLayout(Context context, AttributeSet attrs, int
	 * defStyle) { super(context, attrs, defStyle); }
	 */

}
